package com.tanhua.server.controller;

import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Api(tags = {"登陆相关接口"})
@RestController
@RequestMapping("user")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("01 登录---获取验证码")
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        userService.sendCodeForLogin(phone);
        return ResponseEntity.ok(null);
    }

    @ApiOperation("02 登录---验证码校验")
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> params) {
        String phone = params.get("phone");
        String verificationCode = params.get("verificationCode");
        Map map = userService.login(phone, verificationCode);
        return ResponseEntity.ok(map);
    }

    @ApiOperation("03 首次登录---完善资料")
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfoVo userInfoVo) {
        userInfoService.loginReginfo(userInfoVo);
        return ResponseEntity.ok(null);
    }

    //04 首次登录---补充头像
    @ApiOperation("04 首次登录---补充头像")
    @PostMapping("/loginReginfo/head")
    public ResponseEntity uploadAvatar(@RequestParam MultipartFile headPhoto) {
        userInfoService.uploadAvatar(headPhoto);
        return ResponseEntity.ok(null);
    }
}
